home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / hard / hack / i2clib40.lha / i2clib40 / src / SendI2C.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  2KB  |  66 lines

  1. /* SendI2C.c:
  2.  *   Send bytes supplied in the command line arguments over the I²C bus,
  3.  *   using i2c.library.  Usage:
  4.  * SendI2C  <bus address>  <data>  [<data>  [...] ]
  5.  * where <data> are an arbitrary number of hex digits, in groups of
  6.  * 1, 2, 3 or 4 byte.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <proto/exec.h>
  12. #include <proto/i2c.h>
  13. #include <libraries/i2c.h>
  14.  
  15. struct Library *I2C_Base;
  16.  
  17. int main(int argc, char *argv[])
  18.     {
  19.     int result=0;
  20.     UBYTE adr, i2cdata[ 1000 ];
  21.     UWORD i, j, bytes=0;
  22.     ULONG l, err;
  23.  
  24.     if( argc<3 )
  25.         {
  26.         printf( "Usage: \e[2m%s <addr> <data>\e[0m, ", argv[0] );
  27.         printf( "e.g. %s 70 0027 065b 4f66\n", argv[0] );
  28.         return 5;
  29.         }
  30.     adr = strtoul( argv[1], NULL, 16 );
  31.     for( i=2; i<argc; i++ )
  32.         {
  33.         j = strlen(argv[ i ]);
  34.         if( j & 1 )
  35.             {
  36.             if( j>1 )
  37.                 printf("Warning: odd-length hex number %s\n", argv[i]);
  38.             j++;
  39.             }
  40.         l = strtoul( argv[ i ], NULL, 16 );
  41.         j *= 4;
  42.         while( j>0 )
  43.             {
  44.             j -= 8;
  45.             i2cdata[ bytes++ ] = (l >> j) & 0xff;
  46.             }
  47.         }
  48.  
  49.     I2C_Base = OpenLibrary( "i2c.library", 39 );
  50.     if( !I2C_Base )
  51.         {
  52.         printf( "Can't open i2c.library V39+\n" );
  53.         return 10;
  54.         }
  55.     err = SendI2C(adr, bytes, i2cdata);
  56.     if( (err & 0xff)==0 )
  57.         {
  58.         printf("I²C failure: \e[1m%s\e[0m\n", I2CErrText(err) );
  59.         result = 10;
  60.         }
  61.     CloseLibrary(I2C_Base);
  62.  
  63.     return result;
  64.     }
  65.  
  66.